unit uSolarSystem;

interface

uses  cuSimpleArrays, cuVector,
      uBaseObject, uDefs, uPlanet,

      SysUtils;

type
  TSolarSystem = class( TBaseObject )
  public
    solarStarType      : enumClimate;
    solarStarSize      : integer;

    solarPlanets       : TPlanetList;

    solarFleets        : TIntegerArray; // Links to fleets in system

    solarPopulation    : integer;
    solarGrowth        : integer;
    solarMaxPopulation : integer;

    constructor Create();
    destructor Destroy(); override;
    procedure Reset();

    procedure fleetEnterSolarSystem(theFleetID:integer);
    procedure fleetLeaveSolarSystem(theFleetID:integer);
  end;

  TSolarSystemList = class(TObject)
  public
    Count : integer;
    Items : array of TSolarSystem;

    constructor Create();
    destructor Destroy(); override;
    procedure Reset();

    function addSolarSystem(theSolarId,theX,theY:integer):integer;
    function SystemNameGenerator():string;
  end;

implementation

uses cuDebug, cuFuncs, uSettings;

//================ SolarSystem

constructor TSolarSystem.Create();
begin
  inherited;

  solarPlanets := TPlanetList.Create();
  solarFleets  := TIntegerArray.Create('arrayFleets');
end;
destructor TSolarSystem.Destroy();
begin
  FreeAndNil( solarPlanets );
  FreeAndNil( solarFleets  );

  inherited;
end;
procedure TSolarSystem.Reset();
begin
  inherited;

  solarStarType      := climateNONE;
  solarStarSize      := Random(5)+5;

  solarPlanets.reset();

  solarPopulation    := 0;
  solarGrowth        := 0;
  solarMaxPopulation := 0;
end;
procedure TSolarSystem.fleetEnterSolarSystem(theFleetID:integer);
begin
  solarFleets.addItem( theFleetID );

  DevConsole.Print(LOG_DEBUG2, 'fleetEnterSolarSystem: Fleet #'+IntToStr(theFleetID)+' entered system '+Name+'. Total fleets in system  #'+IntToStr(solarFleets.Count));
end;
procedure TSolarSystem.fleetLeaveSolarSystem(theFleetID:integer);
begin
  solarFleets.DeleteByValue( theFleetID );

  DevConsole.Print(LOG_DEBUG2, 'TSolarSystem.fleetLeaveSolarSystem: Fleet #'+IntToStr(theFleetID)+' left system '+Name+'. Total fleets in system  #'+IntToStr(solarFleets.Count));
end;


//================== SolarSystemList

constructor TSolarSystemList.Create();
begin
  inherited;

  Reset();
end;
destructor TSolarSystemList.Destroy();
begin
  Reset();

  inherited;
end;
procedure TSolarSystemList.Reset();
begin
  Count := 0;
  SetLength( Items, Count);
end;
function TSolarSystemList.addSolarSystem(theSolarID,theX,theY:integer):integer;
var P,maxPlanets,newOrbitPath,newOrbitID:integer; orbitList:TIntegerArray; tempName : string;
begin
  tempName := SystemNameGenerator();
  DevConsole.Print(LOG_DEBUG1, 'addSolarSystem: SolarSystem '+tempName+' (#'+IntToStr(theSolarID)+') charted at Vector #'+IntToStr(theX)+':'+IntToStr(theY) );

  Inc(Count);
  SetLength( Items, Count);

  Items[Count-1] := TSolarSystem.Create();
  Items[Count-1].Reset();
  result := Count-1;

  with Items[Count-1] do
  begin
    orbitList   := TIntegerArray.Create('orbitList'); // Get ready for selecting orbits.
    orbitList.fillArray( 0, SettingsINI.iMaxOrbits-1 );

    Name        := tempName;
    Vector2D.setVector2D( theX, theY );

    case Random(3) of
      0: begin solarStarType := climateYELLOWSTAR; DevConsole.Print(LOG_DEBUG1, 'addSolarSystem: A yellow star has been identified in system '+Name); end;
      1: begin solarStarType := climateREDSTAR;    DevConsole.Print(LOG_DEBUG1, 'addSolarSystem: A red star has been identified in system '+Name   ); end;
      2: begin solarStarType := climateBLUESTAR;   DevConsole.Print(LOG_DEBUG1, 'addSolarSystem: A blue star has been identified in system '+Name  ); end;
    end;

      //== Planet
    with SettingsINI do
      maxPlanets := (iMinPlanets + random((iMaxPlanets+1) - iMinPlanets));

    for P:= 0 to maxPlanets-1 do //-1 due to 0..4 get it?
    begin
      newOrbitID   := Random( orbitList.Count );
      newOrbitPath := orbitList.getItem( newOrbitID );

      DevConsole.Print(LOG_DEBUG3, 'A OrbitIndex #'+IntToStr(newOrbitID)+', OrbitPath #'+IntToStr(newOrbitPath)+', Count #'+IntToStr(orbitList.Count));

      solarPlanets.addPlanet(P, newOrbitPath, Name, solarStarType, solarStarSize);
      orbitList.quickDelete( newOrbitID );
    end;
  end;
end;

{
  Created by: Daniel Mark Avis

  XX-XXX such as F7-XZH (Base this some how on coords)
  Uses hex values 0-9 a-z but letters are first.
  [Quaduant][Sector]-[System] - To use this I would need to fix the positions of the systems. no using Images to create the universe.
  NOTE: Not using numbers yet and no double name check either.
}
function TSolarSystemList.SystemNameGenerator():string;
var I : integer; nstr : string;
begin
  nStr := '';
  for I := 0 to 4 do
  begin
    if (I = 2) then nStr := nStr + '-';

    nStr:= nStr+IntToLetter(Random(26));
  end;
  result := nStr;
end;

end.
